package com.lxyker.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxyker.commonutils.R;
import com.lxyker.eduservice.entity.EduTeacher;
import com.lxyker.eduservice.entity.vo.TeacherQuery;
import com.lxyker.eduservice.service.EduTeacherService;
import com.lxyker.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author lxyker
 * @since 2021-05-20
 */
@Api(tags = {"讲师管理"})
@RestController
@RequestMapping("/eduservice/teacher")
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    //    查询讲师所有数据
    @GetMapping("findAll")
    @ApiOperation(value = "所有讲师列表")
    public R findAllTeacher() {
        List<EduTeacher> list = teacherService.list();
        return R.ok().data("items", list);
    }

    //    逻辑删除 删除讲师
    @DeleteMapping("{id}")
    @ApiOperation(value = "逻辑删除讲师")
    public R removeTeacher(@PathVariable @ApiParam(name = "id", value = "讲师ID", required = true) String id) {
        boolean flag = teacherService.removeById(id);
        return flag ? R.ok() : R.error();
    }

    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(
            @PathVariable long current,
            @PathVariable long limit
    ) {
//        创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);
//        调用方法实现分页
//        调用方法的时候，底层封装，把分页所有数据封装到pageTeacher对象里面
        teacherService.page(pageTeacher);

        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();

        Map map = new HashMap();
        map.put("total", total);
        map.put("rows", records);
        return R.ok().data(map);
    }

    //    条件查询 带分页
    @GetMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(
            @PathVariable long current,
            @PathVariable long limit,
            TeacherQuery teacherQuery
    ) {
//        创建page对象
        Page<EduTeacher> pageTeacher = new Page<>(current, limit);

//        构建条件
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if (!ObjectUtils.isEmpty(name)) {
            wrapper.like("name", name);
        }
        if (!ObjectUtils.isEmpty(level)) {
            wrapper.eq("level", level);
        }
        if (!ObjectUtils.isEmpty(begin)) {
            wrapper.ge("gmt_create", begin);
        }
        if (!ObjectUtils.isEmpty(end)) {
            wrapper.le("gmt_create", end);
        }

//        调用方法，实现条件查询分页
        teacherService.page(pageTeacher, wrapper);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();

        return R.ok().data("total", total).data("rows", records);
    }

    //    添加讲师接口方法
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean save = teacherService.save(eduTeacher);
        return save ? R.ok() : R.error();
    }

    //    根据讲师id进行查询
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id) {
        EduTeacher eduTeacher = teacherService.getById(id);
        return R.ok().data("teacher", eduTeacher);
    }

    //    修改讲师
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher) {
        boolean flag = teacherService.updateById(eduTeacher);
        return flag ? R.ok() : R.error();
    }

    @GetMapping("exceptionTest")
    @ApiOperation(value = "测试手动异常处理")
    public R exceptionTest() {
        List<EduTeacher> list = teacherService.list();

        try {
            int i = 10 / 0;
        } catch (Exception e) {
//            执行自定义异常
            throw new GuliException(20001, "自定义异常处理被执行！");
        }

        return R.ok().data("items", list);
    }
}